//
//  UILabel+Typesetting.m
//  TravelGuideMdd
//
//  Created by Song Xiaofeng on 13-6-7.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "UILabel+Typesetting.h"

@implementation UILabel (Typesetting)

// 调用该函数前确保已经设置了textFont和text
// 1. sizeToFit
// 2. will use (startAnchor, size) to self's frame
// 3. before set frame compute new frame whether exceed limitedAnchor.
//    newFrame.orgin.x + newFrame.size.width < limitedAnchor.x
//    newFrame.orgin.y + newFrame.size.height < limitedAnchor.y
- (CGRect)autoLayoutAt:(CGPoint)startAnchor limitedAnchor:(CGPoint)limitedAnchor;
{
    return [self autoLayoutAtStartX:startAnchor.x startY:startAnchor.y limitedX:limitedAnchor.x limitedY:limitedAnchor.y];
}

- (CGRect)autoLayoutAtStartX:(CGFloat)startX startY:(CGFloat)startY limitedX:(CGFloat)limtX limitedY:(CGFloat)limtY;
{
    CGSize si = [self.text sizeWithFont:self.font];
    CGRect newFrame = CGRectMake(startX, startY, si.width, si.height);
    if (newFrame.origin.x + newFrame.size.width > limtX)
    {
        newFrame.size.width = limtX - newFrame.origin.x;
    }
    
    if (newFrame.origin.y + newFrame.size.height > limtY)
    {
        newFrame.size.height = limtY - newFrame.origin.y;
    }
    
    self.frame = newFrame;
    
    return newFrame;
}

- (CGRect)autoLayoutAtStartX:(CGFloat)starX startY:(CGFloat)startY limitedX:(CGFloat)limtX;
{
    return [self autoLayoutAtStartX:starX startY:startY limitedX:limtX limitedY:UNLIMITED];
}

- (CGRect)autoLayoutAtStartX:(CGFloat)starX startY:(CGFloat)startY limitedY:(CGFloat)limtY;
{
    return [self autoLayoutAtStartX:starX startY:startY limitedX:UNLIMITED limitedY:limtY];
}

- (CGRect)autoLayoutAtStartX:(CGFloat)starX startY:(CGFloat)startY;
{
    return [self autoLayoutAtStartX:starX startY:startY limitedX:UNLIMITED limitedY:UNLIMITED];
}


- (CGRect)autoLayoutAt:(CGPoint)startAnchor;
{
    return [self autoLayoutAtStartX:startAnchor.x startY:startAnchor.y];
}
@end
